<template>
  <el-card class="box-card login-card">
    <div slot="header" class="clearfix">
      <span>请先登录</span>
    </div>
    <el-form ref="form" :model="form" label-position="top" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
          <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
  export default {
    props: {
      id: {type: String}
    },
    data() {
      return {
        form: {
          username: '',
          password: '',
        },
      }
    },
    methods: {
      async onSubmit() {
        let res = await this.$http.post('login', this.form)
        if(res.data.code === 200) {
          localStorage.setItem('token', res.data.token)
          this.$message({
            message: '登录成功！',
            type: 'success'
          });
          this.$router.push('/')
        }
      },
    }
  }
</script>

<style scoped>
  .login-card {
    width: 30rem;
    margin: 10rem auto;
  }
</style>